<?php
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\AdapterWebController;
use App\Models\User\Department;
use App\Models\User\UserClient;
use App\Models\Article\Article;
use App\Modules\Auth\Auth;
use App\Modules\Role\Role;
use App\Modules\User\User;
use App\Modules\Article\ApiArticle;
use Illuminate\Http\Request;
//use Illuminate\Pagination\LengthAwarePaginator;
//use Illuminate\Support\Collection;


class ArticleController extends AdapterWebController
{

    // 添加文章
    public function addArticle(Request $request)
    {
        if($request->isMethod('get')){            
            //获取部门
            $depModel = new Department();
            $depData = $depModel->get();
            if(!empty($depData)){
                $depData = $depData->toArray();
            }
            $ids = implode(',', array_column($depData, 'id'));
            return view('admin.article.add_article',['article'=>1,'articleStatus'=>1,'addArticle'=>1, 'depData' => $depData, 'ids' => $ids]);
        }
        $postData = $request->except('_token');
        $articles = $this->appLayerExec(ApiArticle::class, 'add', $postData);
        flash('添加成功')->overlay();
        return redirect('admin/articleList');
    }

    //文章列表
    public function articleList(Request $request){
        if($request->isMethod('get')){
            $articles = $this->appLayerExec(ApiArticle::class, 'articleList');
            return view('admin.article.articlelist',['list'=>$articles['res'],'article'=>1,'articleStatus'=>1,'articleList'=>1]);
        }
        $postData = array_filter($request->all());
        $articles = $this->appLayerExec(ApiArticle::class, 'articleList', $postData);

        return view('admin.article.articlelist',['list'=>$articles['res'],'article'=>1,'articleStatus'=>1,'articleList'=>1]);
    }

    //文章启用
    public function release($id=0){
        if($id == 2) {
            return view('admin.article.change_article_status');
        }else{
            \App\Models\Article\Article::where('id','=',$id)->update(['release_status'=>1]);
        }
    }


    //文章禁用
    public function cancelRelease($id=0){
        if($id == 3) {
            return view('admin.article.change_article');
        }else{
            \App\Models\Article\Article::where('id','=',$id)->update(['release_status'=>0]);
        }
    }

    //文章删除
    public function deleteArticle($id=0){
        if($id == 4) {
            return view('admin.article.delete_article');
        }else{
            \App\Models\Article\Article::where('id','=',$id)->delete();
        }
    }    

    // 编辑文章
    public function editArticle(Request $request ,$id)
    {
        
        if($request->isMethod('get')){            
            $classify = \App\Models\User\Department::select('id','name_cn')->get();
            if(!empty($classify)){
                $classify = $classify->toArray();
            }
            $info = \App\Models\Article\Article::where('id','=',$id)->get();
            if(!empty($info)){
                $info = $info->toArray();
            }
            return view('admin.article.edit_article',['classify'=>$classify,'info'=>$info[0]]);
        }
        $postData = $request->all();      
        $articles = $this->appLayerExec(ApiArticle::class, 'edit', $postData);
        flash('编辑成功')->overlay();
        $classify = \App\Models\User\Department::select('id','name_cn')->get();
        if(!empty($classify)){
            $classify = $classify->toArray();
        }
        $info = \App\Models\Article\Article::where('id','=',$id)->get();
        if(!empty($info)){
            $info = $info->toArray();
        }
        return view('admin.article.edit_article',['classify'=>$classify,'info'=>$info[0]]);
    }

}